package com.helen.ui.recoveryorder;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helen.adapter.RecoveryOrderListAdapter;
import com.helen.db.UserHelper;
import com.helen.entity.RecoveryOrderEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseFragment;
import com.helen.utils.MyLog;
import com.helen.utils.PhoneUtils;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yang on 2018/4/16.
 */

public class RecoveryOrderFragment extends BaseFragment {
    //1、待处理 2、正在回收 3、回收完毕 4、申请失败
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    private int currentId;
    private int page = 1;
    private View errorView;
    private List<RecoveryOrderEntity> list = new ArrayList();
    private RecoveryOrderListAdapter recoveryOrderListAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.base_fragment_order;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void updateViews() {
        Bundle bundle = getArguments();
        currentId = bundle.getInt("currentId", 1);
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvList.getParent(), false);
        recoveryOrderListAdapter = new RecoveryOrderListAdapter(R.layout.item_recovery_order_list, list);
        recoveryOrderListAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        swipeRefresh.setRefreshing(true);
        swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.appThemeColor));
        rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvList.setAdapter(recoveryOrderListAdapter);
        refresh();

        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        recoveryOrderListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, rvList);


        recoveryOrderListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                RecoveryOrderEntity recoveryOrderEntity = (RecoveryOrderEntity) adapter.getData().get(position);
                Intent ti = new Intent(getActivity(), RecoveryOrderDetailActivity.class);
                ti.putExtra("id", recoveryOrderEntity.getId());
                startActivity(ti);

            }
        });

    }

    private void refresh() {
        page = 1;
        recoveryOrderListApi();
    }


    private void loadMore() {
        page++;
        recoveryOrderListApi();
    }

    private void recoveryOrderListApi() {
        HttpParams params = new HttpParams();
        params.put("id", UserHelper.getInstance().getloginEntity(getActivity()).getId() + "");
        params.put("iid", currentId + "");
        params.put("page", page + "");
        params.put("size", "10");
        MyLog.e("yang", "params==" + params.toString());
        EasyHttp.post(Constants.API_JSONAPI_RECOVERYLIST)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        swipeRefresh.setRefreshing(false);
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "获取回收订单列表失败==" + e.toString());
                        MToast.makeTextShort(getActivity(), getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String stringCacheResult) {
                        MyLog.e("yang", "回收订单列表==" + stringCacheResult);
                        String data = PhoneUtils.checkResponseFlag2(getActivity(), stringCacheResult, TAG);

                        if (data != null) {
                            list = new Gson().fromJson(data, new TypeToken<List<RecoveryOrderEntity>>() {
                            }.getType());
                            MyLog.e("yang", "list.size===" + list.size());
                            if (page == 1) {
                                recoveryOrderListAdapter.setNewData(list);
                            } else {
                                recoveryOrderListAdapter.addData(list);
                            }
                            if (list.size() < 10) {
                                recoveryOrderListAdapter.loadMoreEnd(false);
                                recoveryOrderListAdapter.setEmptyView(errorView);
                            } else {
                                recoveryOrderListAdapter.loadMoreComplete();
                            }

                        }


                    }
                });
    }

    @Override
    public void reloadData() {
        super.reloadData();
        if (mIsMulti) {
            page = 1;
            recoveryOrderListApi();
        }

    }
}
